package com.example.designpattern.behavioral.mediator;

import org.junit.Test;

public class Client {
    @Test
    public void test() {
        Player player1 = new Player();
        Player player2 = new Player();
        Player player3 = new Player();
        Player player4 = new Player();
        // player1 赢了 player3 5 元
        player1.win(player3, 5);
        // player2 赢了 player1 10 元
        player2.win(player1, 10);
        // player2 赢了 player4 10 元
        player2.win(player4, 10);
        // player4 赢了 player3 7 元
        player4.win(player3, 7);

        // 输出：四人剩余的钱：105,120,88,97
        System.out.println("四人剩余的钱：" + player1.money + "," + player2.money + "," + player3.money + "," + player4.money);
    }

    /**
     * 事实上，这段代码还存在一点不足。因为我们忽略了一个前提：微信群里的钱不可以为负数。
     * 也就是说，输家必须先将钱发到微信群内，赢家才能去微信群里领钱。
     * 这个功能可以用我们在 Java 多线程王国奇遇记 中学到的 wait/notify 机制完成，与中介者模式无关，参考juc/kingdom_adventures代码实现
     */
    @Test
    public void test1() {
        Group group = new Group();
        Player player1 = new Player(group);
        Player player2 = new Player(group);
        Player player3 = new Player(group);
        Player player4 = new Player(group);
        // player1 赢了 5 元
        player1.change(5);
        // player2 赢了 20 元
        player2.change(20);
        // player3 输了 12 元
        player3.change(-12);
        // player4 输了 3 元
        player4.change(-13);

        // 输出：四人剩余的钱：105,120,88,97
        System.out.println("四人剩余的钱：" + player1.money + "," + player2.money + "," + player3.money + "," + player4.money);

        System.out.println("微信群里面的钱：" + group.money);

    }

}

